Imports System.Web


Public Class Protocollo
    Inherits Connessione

    Private _id As String
    Private _idColl As String
    Private _numElenco As Int16
    Private _eu As Char
    Private _numero As String
    Private _data As String
    Private _autore As String
    Private _categoria As String
    Private _oggetto As String
    Private _tipo As String
    Private _fascicolo As String
    Private _idFasc As String
    Private _note As String
    Private _mittente As String
    Private _numMittente As String
    Private _dataMittente As String
    Private _dest As String
    Private _destCon As String
    Private _idMitt As String
    Private _idDest As String
    Private _idDestCon As String
    Private _stato As String
    Private _descStato As String
    Private _motivazione As String
    Private _idPratica As String
    Private _pratica As String
    Private _documenti As DataTable


    Public Property id() As String
        Get
            Return _id
        End Get
        Set(ByVal Value As String)
            _id = Value
        End Set
    End Property
    Public Property idColl() As String
        Get
            Return _idColl
        End Get
        Set(ByVal Value As String)
            _idColl = Value
        End Set
    End Property
    Public Property numElenco() As Int16
        Get
            Return _numElenco
        End Get
        Set(ByVal Value As Int16)
            _numElenco = Value
        End Set
    End Property
    Public Property eu() As Char
        Get
            Return _eu
        End Get
        Set(ByVal Value As Char)
            _eu = Value
        End Set
    End Property
    Public Property numero() As String
        Get
            Return _numero
        End Get
        Set(ByVal Value As String)
            _numero = Value
        End Set
    End Property
    Public Property data() As String
        Get
            Return _data
        End Get
        Set(ByVal Value As String)
            _data = Value
        End Set
    End Property
    Public Property autore() As String
        Get
            Return _autore
        End Get
        Set(ByVal Value As String)
            _autore = Value
        End Set
    End Property
    Public Property categoria() As String
        Get
            Return _categoria
        End Get
        Set(ByVal Value As String)
            _categoria = Value
        End Set
    End Property
    Public Property oggetto() As String
        Get
            Return _oggetto
        End Get
        Set(ByVal Value As String)
            _oggetto = Value
        End Set
    End Property
    Public Property tipo() As String
        Get
            Return _tipo
        End Get
        Set(ByVal Value As String)
            _tipo = Value
        End Set
    End Property
    Public Property fascicolo() As String
        Get
            Return _fascicolo
        End Get
        Set(ByVal Value As String)
            _fascicolo = Value
        End Set
    End Property
    Public Property idFasc() As String
        Get
            Return _idFasc
        End Get
        Set(ByVal Value As String)
            _idFasc = Value
        End Set
    End Property
    Public Property note() As String
        Get
            Return _note
        End Get
        Set(ByVal Value As String)
            _note = Value
        End Set
    End Property
    Public Property mittente() As String
        Get
            Return _mittente
        End Get
        Set(ByVal Value As String)
            _mittente = Value
        End Set
    End Property
    Public Property numMittente() As String
        Get
            Return _numMittente
        End Get
        Set(ByVal Value As String)
            _numMittente = Value
        End Set
    End Property
    Public Property dataMittente() As String
        Get
            Return _dataMittente
        End Get
        Set(ByVal Value As String)
            _dataMittente = Value
        End Set
    End Property
    Public Property dest() As String
        Get
            Return _dest
        End Get
        Set(ByVal Value As String)
            _dest = Value
        End Set
    End Property
    Public Property destCon() As String
        Get
            Return _destCon
        End Get
        Set(ByVal Value As String)
            _destCon = Value
        End Set
    End Property
    Public Property idMitt() As String
        Get
            Return _idMitt
        End Get
        Set(ByVal Value As String)
            _idMitt = Value
        End Set
    End Property
    Public Property idDest() As String
        Get
            Return _idDest
        End Get
        Set(ByVal Value As String)
            _idDest = Value
        End Set
    End Property
    Public Property idDestCon() As String
        Get
            Return _idDestCon
        End Get
        Set(ByVal Value As String)
            _idDestCon = Value
        End Set
    End Property
    Public Property pratica() As String
        Get
            Return _pratica
        End Get
        Set(ByVal Value As String)
            _pratica = Value
        End Set
    End Property
    Public Property documenti() As DataTable
        Get
            Return _documenti
        End Get
        Set(ByVal Value As DataTable)
            _documenti = Value
        End Set
    End Property
    Public Property stato() As String
        Get
            Return _stato
        End Get
        Set(ByVal Value As String)
            _stato = Value
        End Set
    End Property
    Public Property descStato() As String
        Get
            Return _descStato
        End Get
        Set(ByVal Value As String)
            _descStato = Value
        End Set
    End Property
    Public Property motivazione() As String
        Get
            Return _motivazione
        End Get
        Set(ByVal Value As String)
            _motivazione = Value
        End Set
    End Property
    Public Property idPratica() As String
        Get
            Return _idPratica
        End Get
        Set(ByVal Value As String)
            _idPratica = Value
        End Set
    End Property




    Public Function getNumero() As String
        Dim s As String = getValore("select numero from numProtocollo where albo =" & HttpContext.Current.Session("albo") & " and anno=" & Year(Today))
        Dim n As Int32
        If s <> "" Then
            n = CInt(s) + 1
            eseguiSql("update numprotocollo set numero=" & n & " where albo =" & HttpContext.Current.Session("albo") & " and anno=" & Year(Today))
        Else
            n = 1
            eseguiSql("insert into numprotocollo (numero, anno, albo) values (" & n & ", " & Year(Today) & "," & HttpContext.Current.Session("albo") & ")")
        End If

        Dim nn As String = Format(n, "000000#")
        Return Right(Year(Today), 2) & "/" & nn

    End Function

    Public Function getMidData(ByVal anno As String) As String
        Return getValore("SELECT   convert(char(10),MAX(Data),103)  AS data " & _
                    " FROM Protocolli where year(data) = " & anno)
    End Function


    Public Sub creaDoc()
        _documenti = New DataTable
        Dim fNomeColumn As DataColumn = New DataColumn
        fNomeColumn.DataType = System.Type.GetType("System.String")
        fNomeColumn.ColumnName = "nome"
        _documenti.Columns.Add(fNomeColumn)
        Dim fFileColumn As DataColumn = New DataColumn
        fFileColumn.DataType = System.Type.GetType("System.String")
        fFileColumn.ColumnName = "nomeFile"
        _documenti.Columns.Add(fFileColumn)
        Dim fHashColumn As DataColumn = New DataColumn
        fHashColumn.DataType = System.Type.GetType("System.String")
        fHashColumn.ColumnName = "hash"
        _documenti.Columns.Add(fHashColumn)

    End Sub

    Public Sub addDoc(ByVal nome As String, ByVal nomeFile As String, ByVal hash As String)
        Dim dr As DataRow = _documenti.NewRow
        dr("nome") = nome
        dr("nomeFile") = nomeFile
        dr("hash") = hash
        _documenti.Rows.Add(dr)

    End Sub

    Public Sub delDoc(ByVal x As String)
        Dim dr As DataRow
        _documenti.Rows(x).Delete()
    End Sub

    Public Sub carica()
        Dim sql As String
        sql = "SELECT idCollegato, EU, Numero, Autore, Categoria, Oggetto, Tipo, Fascicolo as idFasc,  FascProtocollo.descrizione as fascicolo, Note, " & _
                "convert(char(10),data,103) as data, mittente, prot_mittente, destinatario, dest_conoscenza, stato, statoProtocollo.descrizione as descStato, motivazione, pratica as idPratica, " & _
                "convert(char(10),data_mittente,103) as data_mittente, pratiche.nome as pratica " & _
                " FROM Protocolli inner join statoprotocollo on protocolli.stato = statoprotocollo.codice left JOIN FascProtocollo ON Protocolli.Fascicolo = FascProtocollo.id left join pratiche on protocolli.pratica = pratiche.id " & _
                " WHERE Protocolli.ID = " & _id

        Dim dr As DataRow = getDataTable(sql).Rows(0)

        _idColl = Utility.sNull(dr("idCollegato"))
        _eu = dr("eu")
        _numero = dr("numero")
        _data = Utility.sNull(dr("data"))
        _autore = dr("autore")
        _categoria = Utility.sNull(dr("categoria"))
        _oggetto = dr("oggetto")
        _tipo = Utility.sNull(dr("tipo"))
        _fascicolo = Utility.sNull(dr("fascicolo"))
        _idFasc = Utility.sNull(dr("idFasc"))
        _note = dr("note")
        _idMitt = Utility.sNull(dr("mittente"))
        _dataMittente = Utility.sNull(dr("data_mittente"))
        _numMittente = dr("prot_mittente")
        _idDest = Utility.sNull(dr("destinatario"))
        _idDestCon = Utility.sNull(dr("dest_conoscenza"))
        _stato = dr("stato")
        _descStato = dr("descStato")
        _motivazione = Utility.sNull(dr("motivazione"))
        _idPratica = Utility.sNull(dr("idpratica"))
        _pratica = Utility.sNull(dr("pratica"))

        If _idMitt <> "" Then
            _mittente = getValore("select intest + ' ' + nominativo  as mittente from rubrica where id=" & _idMitt)
        End If
        If _idDest <> "" Then
            _dest = getValore("select intest + ' ' + nominativo  as dest from rubrica where id=" & _idDest)
        End If
        If _idDestCon <> "" Then
            _destCon = getValore("select intest + ' ' +  nominativo  as destCon from rubrica where id=" & _idDestCon)
        End If

        _documenti = getDataTable("select nome, nomeFile, hash from docProtocollo where idP = " & _id)

    End Sub


    Public Sub aggDoc()
        eseguiSql("delete from docprotocollo where idP=" & _id)
        Dim x As Int16
        For x = 0 To _documenti.Rows.Count - 1
            eseguiSql("insert into docProtocollo (idP, nome, nomeFile, hash, utente) values (" & _id & ", '" & _documenti.Rows(x)("nome") & "','" & _documenti.Rows(x)("nomeFile") & "','" & _documenti.Rows(x)("hash") & "','" & System.Security.Principal.WindowsIdentity.GetCurrent.Name & "')")
        Next

    End Sub

    Public Sub salva()
        If _id = "" Then
            inserisci()
            aggiorna()
        Else
            aggiorna()
        End If
    End Sub

    Public Sub inserisci()

        _id = eseguiSPInsert("insertProtocollo")

    End Sub

    Private Sub aggiorna()
        Dim sql As String = "update protocolli set idCollegato=@idColl, eu=@eu, numero=@numero, data=@data, autore=@autore, categoria=@categoria, " & _
            "oggetto=@oggetto, tipo=@tipo, fascicolo=@fascicolo, note=@note, data_mittente=@dataMittente, mittente=@mittente, prot_mittente=@numMitt, destinatario=@dest, dest_conoscenza=@destCon, stato = @stato, motivazione = @motivazione, pratica = @pratica, utente=@utente, albo=@albo " & _
            " where id = " & _id
        Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(sql, getConnection)

        cmd.Parameters.Add("@idColl", IIf(_idColl = "", SqlTypes.SqlInt16.Null, _idColl))
        cmd.Parameters.Add("@eu", CStr(_eu))
        cmd.Parameters.Add("@numero", _numero)
        cmd.Parameters.Add("@data", IIf(_data = "", SqlTypes.SqlDateTime.Null, Utility.FormattaDataxDB(_data)))
        cmd.Parameters.Add("@autore", _autore)
        cmd.Parameters.Add("@categoria", IIf(_categoria = "", SqlTypes.SqlInt16.Null, _categoria))
        cmd.Parameters.Add("@oggetto", _oggetto)
        cmd.Parameters.Add("@tipo", IIf(_tipo = "", SqlTypes.SqlInt16.Null, _tipo))
        cmd.Parameters.Add("@fascicolo", IIf(_idFasc = "", SqlTypes.SqlInt16.Null, _idFasc))
        cmd.Parameters.Add("@note", _note)
        cmd.Parameters.Add("@dataMittente", IIf(_dataMittente = "", SqlTypes.SqlDateTime.Null, Utility.FormattaDataxDB(_dataMittente)))
        cmd.Parameters.Add("@mittente", _idMitt)
        cmd.Parameters.Add("@numMitt", _numMittente)
        cmd.Parameters.Add("@dest", _idDest)
        cmd.Parameters.Add("@destCon", _idDestCon)
        cmd.Parameters.Add("@stato", _stato)
        cmd.Parameters.Add("@motivazione", _motivazione)
        cmd.Parameters.Add("@pratica", _idPratica)
        cmd.Parameters.Add("@Utente", System.Security.Principal.WindowsIdentity.GetCurrent.Name)
        cmd.Parameters.Add("@albo", HttpContext.Current.Session("albo"))

        cmd.ExecuteNonQuery()
        cmd.Connection.Close()


    End Sub


    Public Function elencoTipo() As DataTable
        Return getDataTable("select null as codice, '' as descrizione union select codice, descrizione from tipoProtocollo order by descrizione ")
    End Function

    Public Function elencoCategorie() As DataTable
        Return getDataTable("select '' as codice, '' as descrizione union select rtrim(codice) as codice, descrizione from catProtocollo order by descrizione ")
    End Function

    Public Function elencoPratiche() As DataTable
        Return getDataTable("select id,  nome  from pratiche where chiusa = 0 and eliminato=0")
    End Function

    Public Function elencoRubrica(ByVal filtro As String) As DataTable
        Return getDataTable("select id, intest + ' ' + nominativo as nome  from rubrica where eliminato=0 and nominativo like '" & Replace(filtro, "'", "''") & "%'")
    End Function

    Public Function elencoStato() As DataTable
        Return getDataTable("select null as codice, '' as descrizione union select * from statoProtocollo")
    End Function
End Class

Public Class Protocolli
    Inherits Connessione

    Private _proto As Protocollo

    Public Sub New(Optional ByVal newInstance As Boolean = False)

        If (HttpContext.Current.Session("PROTOCOLLO") Is Nothing Or newInstance) Then
            ' new instance here ;)
            _proto = New Protocollo
            HttpContext.Current.Session("PROTOCOLLO") = _proto
        Else
            _proto = HttpContext.Current.Session("PROTOCOLLO")
        End If
    End Sub


    Public Property Proto() As Protocollo
        Get
            Return _proto
        End Get
        Set(ByVal Value As Protocollo)
            _proto = Value
        End Set
    End Property

    Public Function elencoAnni() As DataTable
        Return getDataTable("select distinct year(data) as anno from Protocolli where albo=" & HttpContext.Current.Session("albo") & " order by anno desc")
    End Function

    Public Function elenco(ByVal eu As String, ByVal filtro As String, ByVal ordine As String) As DataTable
        Dim sql As String = "SELECT protocolli.ID, Stato, case when eu='E' then 'Entrata' else 'Uscita' end as eu, Numero, convert(char(10),data,103) as sData, left(Oggetto, 30) as oggetto "
        If eu = "" Then
            sql += " FROM Protocolli where albo = " & HttpContext.Current.Session("albo")
        End If
        If eu = "E" Then
            sql += ", rubrica.nominativo  FROM Protocolli LEFT OUTER JOIN Rubrica ON Protocolli.mittente = Rubrica.id where eu='E' and albo=" & HttpContext.Current.Session("albo")
        End If
        If eu = "U" Then
            sql += ", rubrica.nominativo  FROM Protocolli LEFT OUTER JOIN Rubrica ON Protocolli.destinatario = Rubrica.id where eu='U' and albo= " & HttpContext.Current.Session("albo")
        End If
        sql += filtro
        If ordine <> "" Then
            sql += " order by " & ordine
        End If
        Return getDataTable(sql)

    End Function

    Public Function elencoEu(ByVal eu As String, ByVal filtro As String, ByVal ordine As String) As DataTable
        Dim sql As String
        If eu = "E" Then
            sql = "SELECT Protocolli.ID, stato, 'Entrata' as eu, Numero, ltrim(str(day(data))) + '/' + ltrim(str(month(data))) + '/' + ltrim(str(year(data))) as da, left(Oggetto, 30) as oggetto, Rubrica.Nominativo " & _
                    "FROM Protocolli LEFT OUTER JOIN Rubrica ON Protocolli.mittente = Rubrica.id where eu='E'"
        Else
            sql = "SELECT Protocolli.ID, stato, 'Uscita' as eu, Numero, ltrim(str(day(data))) + '/' + ltrim(str(month(data))) + '/' + ltrim(str(year(data))) as da, left(Oggetto, 30) as oggetto, Rubrica.Nominativo " & _
                "FROM Protocolli LEFT OUTER JOIN Rubrica ON Protocolli.destinatario = Rubrica.id where eu='U'"
        End If
        sql += filtro
        If ordine <> "" Then
            sql += " order by " & ordine
        End If
        Return getDataTable(sql)

    End Function


    Public Sub insertFasc(ByVal livello As Int16, ByVal descrizione As String, ByVal idPadre As String)
        Dim sql As String = "insert into fascProtocollo (descrizione, livello, idPadre) values (@descrizione, @livello, @idPadre)"
        Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(sql, getConnection)

        cmd.Parameters.Add("@livello", livello)
        cmd.Parameters.Add("@descrizione", descrizione)
        cmd.Parameters.Add("@idPadre", idPadre)

        cmd.ExecuteNonQuery()
        cmd.Connection.Close()

    End Sub

    Public Sub updateFasc(ByVal descrizione As String, ByVal idfasc As String)
        Dim sql As String = "update fascProtocollo set descrizione=@descrizione where id=" & idfasc
        Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(sql, getConnection)

        cmd.Parameters.Add("@descrizione", descrizione)

        cmd.ExecuteNonQuery()
        cmd.Connection.Close()

    End Sub

    Public Sub eliminaFasc(ByVal idfasc As String)
        eseguiSql("update fascProtocollo set eliminato=1 where id=" & idfasc)
    End Sub

End Class

Public Class cRubrica
    Inherits Connessione

    Private _id As String
    Private _intest As String
    Private _nome As String
    Private _indirizzo As String
    Private _cap As String
    Private _citta As String
    Private _provincia As String
    Private _telefono As String
    Private _fax As String
    Private _email As String
    Private _note As String


    Public Property id() As String
        Get
            Return _id
        End Get
        Set(ByVal Value As String)
            _id = Value
        End Set
    End Property
    Public Property intest() As String
        Get
            Return _intest
        End Get
        Set(ByVal Value As String)
            _intest = Value
        End Set
    End Property
    Public Property nome() As String
        Get
            Return _nome
        End Get
        Set(ByVal Value As String)
            _nome = Value
        End Set
    End Property

    Public Property indirizzo() As String
        Get
            Return _indirizzo
        End Get
        Set(ByVal Value As String)
            _indirizzo = Value
        End Set
    End Property
    Public Property cap() As String
        Get
            Return _cap
        End Get
        Set(ByVal Value As String)
            _cap = Value
        End Set
    End Property
    Public Property citta() As String
        Get
            Return _citta
        End Get
        Set(ByVal Value As String)
            _citta = Value
        End Set
    End Property
    Public Property provincia() As String
        Get
            Return _provincia
        End Get
        Set(ByVal Value As String)
            _provincia = Value
        End Set
    End Property
    Public Property telefono() As String
        Get
            Return _telefono
        End Get
        Set(ByVal Value As String)
            _telefono = Value
        End Set
    End Property
    Public Property fax() As String
        Get
            Return _fax
        End Get
        Set(ByVal Value As String)
            _fax = Value
        End Set
    End Property
    Public Property email() As String
        Get
            Return _email
        End Get
        Set(ByVal Value As String)
            _email = Value
        End Set
    End Property

    Public Property note() As String
        Get
            Return _note
        End Get
        Set(ByVal Value As String)
            _note = Value
        End Set
    End Property

    Public Sub carica()
        Dim dr As DataRow = getDataTable("select * from rubrica where id = " & _id).Rows(0)
        _nome = dr("nominativo")
        _intest = Utility.sNull(dr("intest"))
        _indirizzo = Utility.sNull(dr("indirizzo"))
        _cap = Trim(Utility.sNull(dr("cap")))
        _citta = Utility.sNull(dr("citta"))
        _provincia = Trim(Utility.sNull(dr("provincia")))
        _telefono = Utility.sNull(dr("telefono"))
        _fax = Utility.sNull(dr("fax"))
        _email = Utility.sNull(dr("email"))
        _note = Utility.sNull(dr("note"))
    End Sub

    Public Sub salva()
        If _id = "" Then
            inserisci()
        Else
            aggiorna()
        End If
    End Sub

    Private Sub inserisci()
        Dim sql As String = "insert into rubrica (intest, nominativo, indirizzo, cap,  citta, provincia, telefono, fax, email, utente,  note) " & _
                " values (@intest, @nome, @indirizzo, @CAP, @citta, @provincia, @telefono, @fax, @email, @utente, @note)"
        Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(sql, getConnection)

        cmd.Parameters.Add("@intest", _intest)
        cmd.Parameters.Add("@nome", _nome)
        cmd.Parameters.Add("@indirizzo", _indirizzo)
        cmd.Parameters.Add("@CAP", _cap)
        cmd.Parameters.Add("@citta", _citta)
        cmd.Parameters.Add("@provincia", _provincia)
        cmd.Parameters.Add("@telefono", _telefono)
        cmd.Parameters.Add("@fax", _fax)
        cmd.Parameters.Add("@email", _email)
        cmd.Parameters.Add("@Utente", System.Security.Principal.WindowsIdentity.GetCurrent.Name)
        cmd.Parameters.Add("@note", _note)

        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
    End Sub

   

    Private Sub aggiorna()
        Dim sql As String = "update rubrica set intest=@intest, nominativo=@nome,  " & _
                " indirizzo=@indirizzo, CAP = @CAP, citta=@citta, provincia=@provincia, telefono=@telefono, fax=@fax, email=@email, " & _
                "  note=@note where id=" & id
        Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(sql, getConnection)

        cmd.Parameters.Add("@intest", _intest)
        cmd.Parameters.Add("@nome", _nome)
        cmd.Parameters.Add("@indirizzo", _indirizzo)
        cmd.Parameters.Add("@CAP", _cap)
        cmd.Parameters.Add("@citta", _citta)
        cmd.Parameters.Add("@provincia", _provincia)
        cmd.Parameters.Add("@telefono", _telefono)
        cmd.Parameters.Add("@fax", _fax)
        cmd.Parameters.Add("@email", _email)
        cmd.Parameters.Add("@Utente", System.Security.Principal.WindowsIdentity.GetCurrent.Name)
        cmd.Parameters.Add("@note", _note)

        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
    End Sub

    Public Sub elimina()
        eseguiSql("update rubrica set eliminato=1 where id=" & _id)
    End Sub
End Class


Public Class cRicerca
    Inherits Connessione


    Private _nuova As Boolean = True
    Private _eu As Char
    Private _categoria As String
    Private _oggetto As String
    Private _tipo As String
    Private _ente As String
    Private _idFasc As String
    Private _idMitt As String
    Private _idDest As String
    Private _stato As String
    Private _mittente As String
    Private _fascicolo As String
    Private _dest As String
    Private _da As String
    Private _a As String
    Private _idPratica As String
    Private _pratica As String


    Public Property pratica() As String
        Get
            Return _pratica
        End Get
        Set(ByVal Value As String)
            _pratica = Value
        End Set
    End Property
    Public Property idPratica() As String
        Get
            Return _idPratica
        End Get
        Set(ByVal Value As String)
            _idPratica = Value
        End Set
    End Property
    Public Property nuova() As Boolean
        Get
            Return _nuova
        End Get
        Set(ByVal Value As Boolean)
            _nuova = Value
        End Set
    End Property
    Public Property eu() As Char
        Get
            Return _eu
        End Get
        Set(ByVal Value As Char)
            _eu = Value
        End Set
    End Property
    Public Property categoria() As String
        Get
            Return _categoria
        End Get
        Set(ByVal Value As String)
            _categoria = Value
        End Set
    End Property
    Public Property oggetto() As String
        Get
            Return _oggetto
        End Get
        Set(ByVal Value As String)
            _oggetto = Value
        End Set
    End Property
    Public Property tipo() As String
        Get
            Return _tipo
        End Get
        Set(ByVal Value As String)
            _tipo = Value
        End Set
    End Property
    Public Property ente() As String
        Get
            Return _ente
        End Get
        Set(ByVal Value As String)
            _ente = Value
        End Set
    End Property
    Public Property idFasc() As String
        Get
            Return _idFasc
        End Get
        Set(ByVal Value As String)
            _idFasc = Value
        End Set
    End Property
    Public Property idMitt() As String
        Get
            Return _idMitt
        End Get
        Set(ByVal Value As String)
            _idMitt = Value
        End Set
    End Property
    Public Property idDest() As String
        Get
            Return _idDest
        End Get
        Set(ByVal Value As String)
            _idDest = Value
        End Set
    End Property
    Public Property stato() As String
        Get
            Return _stato
        End Get
        Set(ByVal Value As String)
            _stato = Value
        End Set
    End Property
    Public Property mittente() As String
        Get
            Return _mittente
        End Get
        Set(ByVal Value As String)
            _mittente = Value
        End Set
    End Property
    Public Property fascicolo() As String
        Get
            Return _fascicolo
        End Get
        Set(ByVal Value As String)
            _fascicolo = Value
        End Set
    End Property
    Public Property dest() As String
        Get
            Return _dest
        End Get
        Set(ByVal Value As String)
            _dest = Value
        End Set
    End Property
    Public Property da() As String
        Get
            Return _da
        End Get
        Set(ByVal Value As String)
            _da = Value
        End Set
    End Property
    Public Property a() As String
        Get
            Return _a
        End Get
        Set(ByVal Value As String)
            _a = Value
        End Set
    End Property

End Class

Public Class Ricerche
    Inherits Connessione

    Private _ricerca As cRicerca

    Public Sub New(Optional ByVal newInstance As Boolean = False)

        If (HttpContext.Current.Session("RICERCA") Is Nothing Or newInstance) Then
            ' new instance here ;)
            _ricerca = New cRicerca
            HttpContext.Current.Session("RICERCA") = _ricerca
        Else
            _ricerca = HttpContext.Current.Session("RICERCA")
        End If
    End Sub


    Public Property Ric() As cRicerca
        Get
            Return _ricerca
        End Get
        Set(ByVal Value As cRicerca)
            _ricerca = Value
        End Set
    End Property
End Class



Public Class cPratica
    Inherits Connessione


    Private _id As String
    Private _nome As String
    Private _desc As String
    Private _chiusa As Boolean

    Public Property id() As String
        Get
            Return _id
        End Get
        Set(ByVal Value As String)
            _id = Value
        End Set
    End Property
    Public Property nome() As String
        Get
            Return _nome
        End Get
        Set(ByVal Value As String)
            _nome = Value
        End Set
    End Property
    Public Property desc() As String
        Get
            Return _desc
        End Get
        Set(ByVal Value As String)
            _desc = Value
        End Set
    End Property
    Public Property chiusa() As Boolean
        Get
            Return _chiusa
        End Get
        Set(ByVal Value As Boolean)
            _chiusa = Value
        End Set
    End Property

    Public Sub carica()
        Dim dr As DataRow = getDataTable("select * from pratiche where id = " & _id).Rows(0)
        _nome = dr("nome")
        _desc = dr("descrizione")
        _chiusa = dr("chiusa")

    End Sub

    Public Sub elimina()
        Dim sql As String = "update pratiche set eliminato=1 where id=" & _id
        eseguiSql(sql)
    End Sub

    Public Sub salva()
        If _id = "" Then
            inserisci()
        Else
            aggiorna()
        End If
    End Sub

    Private Sub inserisci()
        Dim sql As String = "insert into pratiche (nome, descrizione, chiusa, utente) " & _
                " values (@nome, @desc, @chiusa, @utente)"
        Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(sql, getConnection)

        cmd.Parameters.Add("@nome", _nome)
        cmd.Parameters.Add("@desc", _desc)
        cmd.Parameters.Add("@chiusa", _chiusa)
        cmd.Parameters.Add("@Utente", System.Security.Principal.WindowsIdentity.GetCurrent.Name)

        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
    End Sub

    Private Sub aggiorna()
        Dim sql As String = "update pratiche set nome=@nome, descrizione =@desc, chiusa=@chiusa, utente=@utente where id=" & id
        Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(sql, getConnection)

        cmd.Parameters.Add("@nome", _nome)
        cmd.Parameters.Add("@desc", _desc)
        cmd.Parameters.Add("@chiusa", _chiusa)
        cmd.Parameters.Add("@Utente", System.Security.Principal.WindowsIdentity.GetCurrent.Name)

        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
    End Sub


End Class
